package com.jaiky.imagespickers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新的DataHolder，使用单例和弱引用解决崩溃问题
 * <p>
 * Author: nanchen
 * Email: liushilin520@foxmail.com
 * Date: 2017-03-20  07:01
 */
public class DataHolder {
    public static final String DH_CURRENT_IMAGE_FOLDER_ITEMS = "dh_current_image_folder_items";
    private static DataHolder mInstance= new DataHolder();
    private final Map<String, List<ImageItem>> data;

    public static DataHolder getInstance() {
        return mInstance;
    }

    private DataHolder() {
        data = new HashMap<>();
    }

    /**
     * save
     *
     * @param id     the id
     * @param object the object
     */
    public void save(String id, List<ImageItem> object) {
        if (data != null) {
            data.put(id, object);
        }
    }

    /**
     * retrieve
     *
     * @param id the id
     * @return Object
     * @throws RuntimeException when the given source contains invalid encoded sequences
     */
    public Object retrieve(String id) {
        if (data == null || mInstance == null) {
            /**
             * Object
             */
            throw new RuntimeException("你必须先初始化");
        }
        return data.get(id);
    }
}
